package com.kgc.easyshopping.service.Impl;

import com.kgc.easyshopping.dao.UserMapper;
import com.kgc.easyshopping.entity.Order;
import com.kgc.easyshopping.entity.PageInfo;
import com.kgc.easyshopping.entity.Product;
import com.kgc.easyshopping.entity.User;
import com.kgc.easyshopping.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private RedisTemplate redisTemplate;//使用需要注入RedisTemplate
    @Resource
    private UserMapper userMapper;


    @Override
    public PageInfo<Product> selectPageInfo(Map<String, Object> params) {
        PageInfo<Product> pageInfo = new PageInfo<>();
        if(params.containsKey("pageNow")){
            pageInfo.setPageNow((Integer) params.get("pageNow"));
        }
        if(params.containsKey("pageSize")){
            pageInfo.setPageSize((Integer) params.get("pageSize"));
        }
        String name = (String) params.get("loginname");
        List<Product> productList = (List<Product>) redisTemplate.opsForValue().get(name);
        List<Product> productList1 = new ArrayList<>();
        int pageNow = (int) params.get("pageNow");
        int pageSize = (int) params.get("pageSize");
        int start = (pageNow-1) * pageSize;
        int size = start + pageSize;
        for(int i = start;i<size;i++){
            Product product = productList.get(i);
            productList1.add(product);
        }
        pageInfo.setData(productList1);
        int total = productList.size();
        pageInfo.setTotal(total);
        return pageInfo;
    }

    @Override
    public User selectByPrimaryKey(String uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public int selectUser(String loginname,String uid){
        return userMapper.selectUser(loginname,uid);
    }

    @Override
    public int updateByPrimaryKeySelective(User user) {
        userMapper.selectByPrimaryKey(user.getUid());
        return userMapper.updateByPrimaryKeySelective(user);
    }
}
